package com.example.auth.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.auth.entity.AppClient;
import com.example.auth.response.Result;
import com.example.auth.service.AppClientService;
import com.example.auth.util.AppUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/auth-clients")  // 授权客户端信息 API 路径
public class AuthClientController {

    @Resource
    AppClientService clientService;
    @Resource
    PasswordEncoder passwordEncoder;

    @PostMapping("/add")
    public Result addAuthClient(@RequestBody AppClient appClient){
        return clientService.addAuthClient(appClient);
    }

    @GetMapping(value = "/list")
    public Result<?> queryPageList(
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        LambdaQueryWrapper<AppClient> queryWrapper =new LambdaQueryWrapper<>();
        Page<AppClient> page = new Page<AppClient>(pageNo, pageSize);
        IPage<AppClient> pageList = clientService.page(page, queryWrapper);
        pageList.getRecords().stream().forEach(x-> x.setClientSecret(AppUtils.getAppSecret(x.getClientId())));
        return Result.OK(pageList);
    }

}
